How to generate custom page names in wordpress? - php

I lost my old website's code and am trying to recreate a function I had created ages ago.
I have custom pages in my Wordpress blog: about, work, contact, news as well as posts and subpages.
I am trying to manipulate the value of <h1 class="site-title"><?php bloginfo( 'name' ); ?></h1> to display those different page tiles for those pages and their children so that it would look something like this:
if( $show == 'name' )
{
if( is_page('work') ) $output = 'Work';
if( is_page('contact') ) $output = 'Contact';
if( is_page('about') ) $output = 'About';
}
return $output;
And as for all the rest of the pages, I just want it to display the default site value so somewhere along the line there would be an
else $output = "Site Name";
So I kind of understand the logic because I have done it in the past but cannot remember how the syntax went.
Could anyone refresh my memory?

You could put the following in your functions.php file:
function so20527793_sitename()
{
$output = get_bloginfo( 'name' );
if( is_page( 'work' ) ) $output = 'Work';
if( is_page( 'contact' ) ) $output = 'Contact';
if( is_page( 'about' ) ) $output = 'About';
return $output;
}
Usage:
<h1 class="site-title"><?php echo so20527793_sitename(); ?></h1>

Well I did not get much help in the end so I had to figure it out by googling and combining functions. Here is the result:
function is_child($page_id_or_slug) {
global $post;
if(!is_int($page_id_or_slug)) {
$page = get_page_by_path($page_id_or_slug);
$page_id_or_slug = $page->ID;
}
if(is_page() && $post->post_parent == $page_id_or_slug ) {
return true;
} else {
return false;
}
}
function Custom_PageName(){
$output = get_bloginfo( 'name' );
if (is_page('about') || is_child('about')) $output = 'About title';
if (is_page('work') || is_child('work')) $output = 'Work Page';
if (is_page('contact') || is_child('contact')) $output = 'Contact';
return $output;
}

Related

Creating a wordpress virtual page with a function and page template

I'm trying to create a virtual contact page on wordpress. All the necessary data will be stored in a page template. But in my function I don't know how to link to the file.
When the user does to domain.com/contact I would like to redirect him to the contact.php file.
So far I got this function to work and show content from this line $post->post_content = 'page template: contact.php'; but I would like to show the content from templates/contact.php
add_filter( 'the_posts', 'generate_contact_page', -10 );
function generate_contact_page( $posts ) {
global $wp, $wp_query;
$url_slug = 'contact'; // URL slug of the contact page
if ( ! defined( 'CONTACT_PAGE' ) && ( strtolower( $wp->request ) == $url_slug ) ) {
// stop interferring with other $posts arrays on this page (only works if the sidebar is rendered *after* the main page)
define( 'CONTACT_PAGE', true );
// create a contact virtual page
$post = new stdClass;
$post->post_author = 1;
$post->post_name = $url_slug;
$post->guid = home_url() . '/' . $url_slug;
$post->post_title = 'Contact page';
$post->post_content = 'page template: contact.php';
$post->ID = -999;
$post->post_type = 'page';
$post->post_status = 'static';
$post->comment_status = 'closed';
$post->ping_status = 'open';
$post->comment_count = 0;
$post->post_date = current_time( 'mysql' );
$post->post_date_gmt = current_time( 'mysql', 1 );
$posts = NULL;
$posts[] = $post;
// make wpQuery believe this is a real page too
$wp_query->is_page = true;
$wp_query->is_singular = true;
$wp_query->is_home = false;
$wp_query->is_archive = false;
$wp_query->is_category = false;
unset( $wp_query->query[ 'error' ] );
$wp_query->query_vars[ 'error' ] = '';
$wp_query->is_404 = false;
}
return $posts;
}
How can I achieve something like this? Maybe you know a better solution ?
So for template output you can try this function
function get_template_output($slug = '', $name = '') {
ob_start();
get_template_part($lug, $name);
return ob_get_clean();
}
And then to assign content you will use
$content = get_template_output('templates/', 'contact');

(Wordpress) How to add a simple text to the contents of a specific category (php)

the following code in my function.php can almost do the job nice and clean to all single pages. the problem is I want it to be filtered for a specific category ID:
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Your new content here';
}
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
You don't need to use get_the_category and examine the results - Wordpress already has a function to check if your post is in a category: has_category. The advantage of this over get_the_category is that
you don't need the code to loop through all results to comparing the id (this is the part that is wrong in the other answer - it will only work if your post has one single category)
You can use not just the ID, but also the slug or name
You can check for multiple categories
The code you need is very simple, you just need to change one line!
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
/* pass an array with the IDs/names/slugs of the categories to check for, e.g. */
if ( has_category( array(12) ) )
$content .= 'Your new content here';
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
UPDATE:
Even though it wasn't in your question, if you actually want to include the content on all posts as well, you just need to do this:
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
$content .= 'Your new content here';
if ( has_category( array(12) ) )
$content .= '<br>Your new content here';
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
Modified your code idealy this condition is never making any sense because it always getting the id. $GLOBALS['post']->ID == get_the_ID()
function wp_add_to_content( $content ) {
if( is_single() && ! empty( $GLOBALS['post'] ) ) {
if ( $GLOBALS['post']->ID == get_the_ID() ) {
$content .= 'Your new content here';
}
//getting the current post
global $post;
$category_detail=get_the_category( $post->ID );
if(!empty($category_detail))
{
$catId = $category_detail[0]->term_id;
// instead of 2 put your category id
if($catId == 2 && $catId != 0 )
{
$content .= 'Your new content here';
}
}
}
return $content;
}
add_filter('the_content', 'wp_add_to_content');
Note : above code is for only default post not for custom taxonomy for custom taxonomy

Multiple if Statements issue

I am having a problem with multiple if statements. I am using && but it seems to only work for the first statement.
The code is like such:
global $post;
$args = array( 'post_id' => $post->ID );
$comment = get_comments( $args );
$user = wp_get_current_user();
if ( 3 <= count( $comment ) && $post->post_author == $user->ID) {
echo do_shortcode( '[button]' );
} else {
comment_form();
}
It basically stats that if there is less than 3 comments then show the comment form but if there is more than 3 and is the post author then show a button. The button shows but only if there are more than 3 comments. It doesn't check if it is only the post author or not, like I want it to.
What you are describing are two cases in which the button should be showed. So there have to be two ways to get into the if-block. You have to refactor your if-statement with the logical or-operator ||.
for example:
if($post->post_author == $user->ID || 3 <= count($comment)) {
echo do_shortcode( '[button]' );
} else {
comment_form();
}
I had to change the code like so to get it to work.
global $post,$current_user;
$args = array( 'post_id' => $post->ID );
$comment = get_comments( $args );
get_currentuserinfo();
if ($post->post_author == $current_user->ID ) {
echo do_shortcode( '[button]' );
} elseif ( 3 <= count( $comment ) ) {
// blank
} else {
comment_form();
}

How to add <title> to Custom Pages in WP's function.php?

I'm using a wordpress theme that creates custom pages.
However, when SEO Yoast is used to make custom title and meta descriptions, all the custom pages have the same title tags as index.php (the homepage). This is a red flag for google webmaster tools obviously.
add_action('theme_query', 'theme_page_query');
function theme_page_query() {
global $wp_query;
$act = get_query_var( 'act' );
if ($act == 'new-wallpapers') {
query_posts('orderby=post_date&order=DESC&paged='.get_query_var('page'));
} elseif ($act == 'popular-wallpapers') {
query_posts('meta_key=theme_hit_download&orderby=meta_value_num post_date&order=DESC&paged='.get_query_var('page'));
} elseif ($act == 'random-wallpapers') {
query_posts('orderby=rand&paged='.get_query_var('page'));
} elseif ($act == 'recent-downloads') {
query_posts('meta_key=theme_hit_download_time&orderby=meta_value&order=DESC&paged='.get_query_var('page'));
}
}
function theme_page_title($title) {
if (in_array(get_query_var( 'act' ), array(
'new-wallpapers',
'popular-wallpapers',
'random-wallpapers',
'recent-downloads',
))) {
$title = ucwords(str_replace('-', ' ', get_query_var( 'act' )));
}
return $title;
}
add_filter( 'wp_title', 'theme_wp_title', 10, 2 );
function theme_wp_title( $title, $sep ) {
global $paged, $page, $wp_query;
if ( is_feed() ) {
return $title;
}
if (in_array(get_query_var( 'act' ), array(
'new-wallpapers',
'popular-wallpapers',
'random-wallpapers',
'recent-downloads',
))) {
$title = ucwords(str_replace('-', ' ', get_query_var( 'act' ))) . " {$sep} ";
}
// Add the site name.
$title .= get_bloginfo( 'name' );
// Add the site description for the home/front page.
$site_description = get_bloginfo( 'description', 'display' );
if ( $site_description && ( is_home() || is_front_page() ) ) {
$title = "$title $sep $site_description";
}
// Add a page number if necessary.
if ( $paged >= 2 || $page >= 2 ) {
$title = "$title $sep " . sprintf( __( 'Page %s', 'theme' ), max( $paged, $page ) );
}
return $title;
}
I have tried searching, but I cannot find a step-by-step guide and I am not good in PHP. Any help would be much appreciated.
You can do this in three steps by
1.Add a new input box to the admin pages for creating and editing new Posts/Pages
2.Save the the value of that input box when that Post/Page is saved
3.Echo out a new title as part of the wp_head() function
here is the detailed explanation..Hope it helps ..Thanks

Image in recent post of Wordpress Q and A theme

i am using Instant-QA theme, need to update the recent Post widget with image of the user who has asked the questions. I am using the below code in functions.php of subdomain of wordpress website. Currently its showing the simple post--- Any guess in below code-- ?
function print_requested_template_part() {
// Respond only to requests from the same address...
if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['including_template_part']) && isset($_GET['load_part']) && $_GET['load_part'] != '' ) {
$part = $_GET['load_part'];
$func = 'render_' . str_replace('-', '_', $part); // if you have declared a function called "render_footer_include", then "?load_part=footer_include"
if ( function_exists($func) ) {
// Allow for passing parameters to the function
if ( isset($_GET['params']) ) {
$params = $_GET['params'];
$params = ( strpos($params, ',') !== false )? explode(',', $params) : array($params);
call_user_func_array($func, $params);
} else {
call_user_func($func);
}
}
exit; // if we don't exit here, a whole page will be printed => bad! it's better to have empty footer than a footer with the whole main site...
}
}
add_action('init', 'print_requested_template_part', 1);
function render_my_recent_posts( $numberposts = 5 ) { ?>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul><?php
}
DO not waste you time to indulge urself in the above code. Usel below plugin and gets the immediate output.
http://wordpress.org/extend/plugins/recent-comments-with-avatars/ Use this plugin
Thankyou

Categories