Questions tagged [conditional-content]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
80 votes
10 answers

Enqueue Scripts / Styles when shortcode is present

What is the idea way to register/enqueue scripts and/or styles for use in plugins? I recently made a plugin simple plugin to add the user avatar/gravatar with a shortcode. I have different style ...
  • 4,279
136 votes
8 answers

How do test if a post is a custom post type?

I am looking for a way to test if a post is a custom post type. For example, in, say, the sidebar I can put in code like this: if ( is_single() ) { // Code here } I want code testing for only ...
17 votes
4 answers

Loading scripts only if a particular shortcode or widget is present

I needed a way to filter a page/posts content before it was loaded so I could add scripts to the header if a specific shortcode was present. After much searching I came across this on http://...
  • 1,161
1 vote
1 answer

Exclude custom function content from certain pages

I have added a small custom function to display a little author bio blurb at the bottom of each blog entry in my functions.php file inside the current theme that I am using. So far so good, it is ...
  • 113
15 votes
9 answers

Why isn't is_page working when I put it in the functions.php file?

I have page called "Apple", the page's ID id 2533. In page.php file I have line: echo $bannerimg And this function in functions.php: if ( is_page( '2533' ) ) { // also tested with 'Apple' ...
  • 4,991
34 votes
8 answers

wp enqueue style on specific page templates

I am in the process of a theme, I would like to add landing pages using page-templates. I cannot find anywhere that shows how to enqueue style or js for specific page templates. Any suggestions. Ex. ...
  • 441
7 votes
2 answers

how to create a conditional content_width for a wordpress theme?

I'm trying to create a conditional statement for the content width in the functions.php of my theme. So some categories will have a content_width of 580, some will have 900. This will make the oEmbed ...
6 votes
7 answers

wp_enqueue_script adding conditional statement not working

This is the code im using in my functions file: add_action('init', 'sort_out_jquery_pngfix_frontend'); function sort_out_jquery_pngfix_frontend() { global $wp_scripts; if(!is_admin()) { ...
  • 12k
11 votes
3 answers

How to conditionally enqueue a stylesheet only for a certain page(s)?

Before I get started, I want to acknowledge that there are some really good articles that are targeted at how to conditionally enqueue scripts based on page/post content. WordPress Plugin Development ...
5 votes
1 answer

How can I determine if a post has an image attachment?

As far as I know, there's not a simple function that will return true if a post has an attachment. With that in mind, what's the best way to determine if a post has an attachment (or even better, has ...
3 votes
1 answer

Show/hide Widgets in Dashboard Based on Current Advanced Custom Fields Option

So, in an effort to move my new project forward, I'm looking for a way to register and de-register widget areas depending on which header file I load via ACF Pro in the Options page. A bit of info ...
3 votes
1 answer

Can shortcodes contain conditional statements? Even without them my shortcode renders blank page

The following code is supposed to allow for a shortcode that includes many attributes, but not all of the attributes may always be present in which case I do not want the containing element to show ...
  • 890
3 votes
4 answers

Show content after the first and second paragraph

The function below is used to show certain content after the first paragraph. I would like to show 'content X' after the 1st paragraph and 'content Y' after 2nd paragraph. <?php $paragraphAfter= 1;...
  • 317
2 votes
1 answer

If function exists, and array is met, echo function?

I have a function like the following: <?php echo my_custom_function( array( 'category_url'=>'My Special Category' ) ); ?> It pulls data from the "My Special Category" and displays it. I ...
  • 1,697
1 vote
5 answers

meta content on required pages

I need to post extra information on programs pages, so i have decided to use wordpress metabox but its showing on all pages. So is there any condition check so it could put meta box on those sub pages ...
  • 165
1 vote
2 answers

is_front_page, is_page('slug'), is_page(id) not working

None of the following are working to display conditional format in the sidebar when the current page is the home page: is_front_page, is_page('home'), is_page(id) I added an else clause which is ...
  • 1,666
1 vote
2 answers

Add default content to posts in a specific category?

Update / Question clarification: How could you modify the following code from this thread: Force category choice before creating new post? which offers a drop-down menu to choose a category BEFORE ...
0 votes
1 answer

Shortcode to Gutenberg-block: additional text on front-end and conditional display

I'm working to translate a custom shortcode to a Gutenberg-block but am stuck on two issues. This post first treats the current situation, the next heading shows the problems I'm running into in ...
  • 276
0 votes
1 answer

How to hide <pre> and <table> content from auto-generated excerpts?

Auto-generated excerpts just take the first x characters from the post content and cut that off at a certain point. When there is a table or pre element in that part, the excerpt looks rather useless ...
  • 106k
0 votes
2 answers

Is there a PHP function that can match anything after the given URL, so my IF statement will work in each instance?

I have an IF statement shown below that runs one of 2 custom queries, based on the url. The problem I'm facing is if the url contains pagination such as /page/2/, my query breaks because the url isn't ...
  • 1,163
0 votes
2 answers

Conditional Statement - check if post has an attachment image

I'm working on a conditional statement (in a loop) that looks for a featured image then grabs a default image if there is none, which I have figured out. But, I also wanted a way to pull the first ...
  • 1,496
0 votes
1 answer

How Can I Register Menus and Widgets Conditionally Based on Theme Options/Settings?

I have theme options/settings page and options/settings fields set as outlined here:
  • 349