How to implement conditional CSS in this situation? - php

Am using this function
http://bavotasan.com/2012/a-better-wp_link_pages-for-wordpress/
And calling it within template with this
<center><span class="post-pagination"><?php custom_wp_link_pages(); ?></span></center>
However, the CSS shows even on pages that don't contain multipaged articles.
So I guess I need something like
<?php if $multipage then <center><span class="post-pagination"><?php custom_wp_link_pages(); ?></span></center><?php endif; ?>
But obviously this would result in errors.
Need help with syntax for including in the template or proper way of implementing it into the function itself.
$multipage is a variable that already exists and contains the data that tells if the current page is multi or no.

$multipage is a global variable, so you can use following;
<?php
if ( $multipage ) :
?>
<center><span class="post-pagination"><?php custom_wp_link_pages(); ?></span></center
<?php
endif;
?>

From my point of view, first you create the template for the page in which you need pagination.
Then select that newly created template in page. That's it.

Related

Wordpress single.php another get_post function destory the first function

I am trying to deal with two get_post functions, in single.php the first get_post function is the post from the wordpress, But after that I called get_post function to other post also to use both of them in the same page but after I call the first get_post ( the main post ) I get the only second data and cant reach the first data.
My code called to the second function ( The first is from wordpress post):
$main_post = get_field('main_post');
$main_p = get_post($main_post->ID);
Then I am trying to use the variable $post OR the_title() OR any other functions to get the first post and it always returning the info of the $main_p post
for example
get_the_title( get_post()->ID )
returns the $main_p post id and not the main post of the single.php
any soulutions ?
I may be wrong, but it seems to me that you are trying to post a different post format with normal post format?
I, myself use get_post_format() so it can be styled differently or have different options.
in single.php I use
<!-- checking if there are any blogposts to be shown using have_posts check which is a wordpress function-->
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?> <!-- the correct syntax for while loop in wordpress to show all the blogposts -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : //else stament if there aren't any posts (put inside if before endif)?>
<p><?php __('No Posts Found'); ?></p>
<?php endif; ?> <!-- stop checking for blog posts-->
</div><!-- /.blog-main -->
Inside functions.php I activated the post-formats inside wp_theme_setup() function
add_theme_support('post-formats', array('aside', 'gallery'));
In this case i activated the gallery and aside posts
That way I have 2 different post types on one page
like this
image from my theme blog page
Here is also a video tutorial on post formats from Traversy media
https://www.youtube.com/watch?v=CRa7eiqyiCM&list=PLc5p9nvpdwBlrNU0hr1f0kXPRkh0aGo1Q&index=7
The key reason why your post values are being overwritten, the additional get_post() declarations are overriding the default query. Now, the code in your pastebin is a pretty massive dog's breakfast, so a direct solution is a rather large undertaking (e.g. the indentation is all over the place, the code snippets are less than ideal regarding their readability, etc...). However, I can point you in the right direction for the solution.
When I pull content from another page on my WordPress sites, I avoid using get_post() in favour of declaring a fresh new WP_Query() (that's just my preference), following it up with a wp_reset_postdata() declaration.
Here's an example of multiple queries on a single template in the WordPress codex:
https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
The key here is the wp_reset_postdata(). I'd recommend looking into it's purpose. It'll save you a lot of grief:
https://codex.wordpress.org/Function_Reference/wp_reset_postdata

Displaying first few words of WordPress page in the nav

I'm wanting to display a WordPress nav that has the first few words of each page below it.
This is what I currently have:
<?php wp_list_pages('title_li=&link_after=<span>FIRST FEW WORDS HERE</span>'); ?>
I want the output to be like:
<li>Home<span>Welcome to the website</span></li>
Any help will be appreciated
The only thing I could think of is doing this:
use wp_get_pages to get all the titles (or the ones you want), and then access the database with a query that will get each post using the title. So, for each title from the array the get_pages returned, you get a corresponding post. In either the SQL or the PHP use the substring method to get the first few words, and display however you want.
That's just the logic, I'm a bit rusty with code so I don't want to confuse you. I'll try if you don't get it...
I don't think there's a way to put extra text inside the anchor by just passing params to wp_list_pages. You could however filter it and use preg_replace. For example...
add_filter('wp_list_pages', function($data) {
return preg_replace('/(<a[^>]+[^<]+)<\/a>/si', '\\1<span>FIRST FEW WORDS HERE</span></a>', $data);
});
Though I used preg_replace() you could also (and probably should) use DomDocument but this will work for most use cases.
Another way to do it would be to use a custom walker
Another option is to make a custom loop and add it in the "index.php" file in your theme where you want to display it.
A quick example of how that would look:
<div id="nav"><ul>
<!-- start the loop-->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="nav_post">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?>
<span><?php formatPreview(the_content()); ?></span></a>
</li>
<!-- stop the loop-->
<?php endwhile; else: ?>
<p>No posts!</p>
<?php endif; ?>
</ul></div>
the_permalink(), the_title(), and the_content() are the key players here to populate your custom nav with the post information. Then just add appropriate css for the nav ID, nav_post class, and h2/p and you're good to go! You'll probably want to hard stop the loop after a certain number of posts - I doubt you want every post listed in the nav!
You'll have to make the "formatPreview()" function separately to control how to format the preview of each post's content - but it shouldn't be hard to just substr the first few words!

Display posts on tag.php template?

Is it possible to create a tag.php template that when you navigate to the url www.domain.com/tag/architecture/ it will display all the custom posts that have been tagged with that specific tag? And so on for various other tags?
What code will I need to include on my template?
Yes You can create,below is the code i used to display custom post type "knowledge"
<?php
global $query_string;
$posts = query_posts($query_string.'&post_type=knowledge');
if ( have_posts() ) while ( have_posts() ) : the_post();
?>
<?php the_title(); ?>
<?php endwhile; // end of the loop. ?>
This will help you understand hierarchy of templates:
http://codex.wordpress.org/images/1/18/Template_Hierarchy.png
Use of $query_string (example) available here:
https://developer.wordpress.org/reference/functions/query_posts/
According to the template hierarchy, you can create a file called tag.php which will be used instead of index.php if a tag page is displayed. You can also prepare separate templates for specific tags.
The best way is to start by creating a copy of your theme's index.php and calling it tag.php, this way you'll have some basic code working. Then you can modify tag.php to fit your needs - probably by editing The Loop, or maybe changing some includes if your theme loads the loop from separate file. (but in this case you should consider editing index.php to load some other loop for tag pages - is_tag() may come in handy)

How to have different logic for a page and a post within Wordpress?

I am modifying a wordpress template and need to slightly separate rendering logic for a post and a page, specifically in relation to how the date renders out. The problem is that I cannot find any code to do this, I am sure that it exists. Is there a variable that exists within wordpress that tells me whether the item being displayed is a page or a post?
In an ideal world it would look something like this:
<?php if (is_page()) : ?>
page logic
<?php else: ?>
post logic
Would appreciate any help!
Pages are a type of post, so get_post_type should return appropriately different values for pages vs normal blog posts.
I found this link: http://wordpress.org/support/topic/sidebar-logic-for-postblogroll-and-page-type which seemed to do the business for me.
The answer (copied straight from the page) was:
<?php if(is_singular($post)): ?>
Page Content
<?php else:?>
Post Content
<?php endif;?>

Run php functions on selected Wordpress pages

I am trying to customise a Wordpress theme. I have a function in themes/functions.php that I would like to run on some pages.
I need to be to:
Detect the page ID to determine whether the function should execute
Determine which hook to attach the function to (preferably something like page load.
Cheers
The file functions.php is for theme specific functions called inside your theme. If this is a theme specific function then the function call should be in the header (or wherever you want the output of the function to appear) via <?php my_function() ?>.
Hooks are for plugins, not template specific code.
If you are inside The Loop, then you can call <?php the_ID(); ?> as WarrenB said. If you are outside of the loop, then <?php echo $post->ID?> will print the page ID.
To the questions you posed, given you want to select on id #9, run your loop like this:
<?php
query_posts('page_id=9');
if (have_posts()) : while (have_posts()) : the_post();
// Do whatever on post id #9
?>
<?php endwhile; else: ?>
// Do whatever on all the other posts
<?php endif; ?>
If this isn't the answer you're looking for, please add more information to your question.

Categories