wp_title() not working properly with blog in non-root folder - wordpress

I followed this guide to have my blog appear under mydomain.com/blog:
http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder
In short, I'm using a custom page template to create a static page "Blog", which then goes and renders the posts:
<?php
/*
Template Name: Blog
*/
// Which page of the blog are we on?
$paged = get_query_var('paged');
query_posts('cat=-0&paged='.$paged);
// make posts print only the first part with a link to rest of the post.
global $more;
$more = 0;
//load index to show blog
load_template(TEMPLATEPATH . '/index.php');
?>
In my theme, I render the <title> tag using the following syntax:
<title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(''); ?></title>
This is described here in the codex:
http://codex.wordpress.org/Function_Reference/wp_title#Covering_Homepage
The problem is that now on mydomain.com/blog, there is no title rendered by wp_title('');
If I stop using the blog.php template, then the title appears correctly. Obviously the blog posts do not anymore though. How to have the wp_title() display the correct title in this situation?

You should be following the updated guide for Wordpress 2.1+:
http://codex.wordpress.org/Creating_a_Static_Front_Page

Related

Use a Wordpress Page as a stylesheet?

I'd like to be able to edit my WordPress theme's CSS by using a Page in the backend instead of the default style.css file. I've seen it done before as a page template but I can't seem to figure it out myself.
I'm using WordPress version 4.7.2 on a Multisite. I want the CSS to generate on theme activation, hence using a Page.
I apologize in advance if this is an easy fix and open to other ways to accomplish this. Thanks!
I highly recommend using a plugin, such as https://wordpress.org/plugins/wp-add-custom-css/ or use the WordPress Customizer "Additional CSS" field.
However, to answer your question, this was easy since it's a modified way that I make some pages I load with ajax. You will need to create a page template and that requires FTP and a code editor to create the page and then push it your theme folder (hopefully a child theme or this template will go away when you upgrade).
I named my CSS page template: css-page.php
It requires only the opening php tag.
CODE for template css-page.php
<?php
/**
*
* Template Name: CSS page
* Not a recommended idea.
*
*/
header("Content-type: text/css; charset: UTF-8");
ob_start("compress");
//minify CSS
function compress( $minify ) {
/* remove comments */
$minify = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $minify );
/* remove tabs, spaces, newlines, etc. */
$minify = str_replace( array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $minify );
return $minify;
}
//get the content
if (have_posts()) :
while ( have_posts() ) : the_post();
$content = get_the_content();
$content = wp_filter_nohtml_kses( $content );
echo $content;
endwhile;
endif;
ob_end_flush();
Then in your functions.php file in your child theme (or an include of that file or a custom plugin) paste in the following BUT change it where noted:
Change the value p='3134' to your page id where you're using this template. Change the id="something" to something else or remove it. Useful to have an id or a class for js manipulation.
For functions.php
//Get CSS page contents
function myprefix_page_css() {
echo '<link rel="stylesheet" id="something" href="'. site_url() .'/?p=3134" type="text/css" media="screen">';
}
add_action( 'wp_head', 'myprefix_page_css', 99 );

Different header in different pages in wordpress

I am going to develop a WordPress theme for my own site. It has three types of menu.
first one is only for the home page, the second one is for any normal page (like about us, contact etc.) and the third one is for article page (when a news will be clicked then where one will be gone).
how can I do this?
thanks.
There are multiple ways you can go about doing this. For the article I would create a PHP file named page-article.php and within the file follow what developer tells you:
<?php
/**
* Template Name: Article
*
*/
and then create a custom header for the article in a header-article.php file:
header('article') ?>
To target the home you could use is_page() and pass it home but I would suggest using is_home() instead but you could always pass both like:
if ( is_page('home') || is_home() ) :
// further code
endif;
Do note this would depend on your settings as you do not mention if you will have a custom page set by the setting under Settings -> Reading so if you set front page you should target it with is_front_page():
if ( is_page('home') || is_home() || is_front_page() ) :
// further code
endif;
You could always code all of this in one header.php file but I would suggest creating an additional header file and calling it header-article.php
You can do it using is_page() function.
See full documentation here
if( is_page( array( 'about-us', 'contact') ){
//display your page menu
}
if( is_page( 'home' ){
//display your home page menu
}
if( is_page( 'article' ){
//display your article page menu
}else{
//display default menu
}
If you need different header in different page then you have to create a page template then you can add different header file in different page template-
Normally we use get_header() to include header.php
Now create a copy of header.php file and named it header-alternative.php
Then create a page template named "Alternative header"
<?php get_header('alternative');
/*
*Template Name: Alternative Header
*/
?>
<section class="error-page-inner">
<h1 class="error-code"><?php esc_html_e( '404', 'labstore' ); ?></h1>
</section>
<?php get_footer(); ?>
Then header-alternative.php is called for this page template.

Displaying page content

I have two codes. The first one I am using on page.php and it displays content from any pages I create in the admin panel. The second code works to display my posts on the mainpage just not sure where that code should go.
It works if I place it in the page.php but then the same content (posts) are shown on any pages I create. I tried placing the second code in home.php and index.php at the same time as using the first code in page.php but does not work.
if (have_posts()): while (have_posts()): the_post();
wp_title(''); echo '<br />';
the_content(); echo '<br />';
endwhile; endif;
<--- Second Code -->
$args = array( 'posts_per_page' => 10 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
setup_postdata( $post ); ?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail( array(100,100) ); ?></div>
<div id="pcontent">
<?php the_title(); ?>
<?php the_excerpt(); ?><br />
Post Category: <?php the_category( ', ' ); ?>
</div>
</div>
<?php endforeach;
wp_reset_postdata(); ?>
I think you should take a look at the following: https://developer.wordpress.org/themes/basics/template-hierarchy/#home-page-display
You'll notice that, if present, home.php is the file that WP will use to display your posts page - if of course a page separate to your normal front page has been set in Settings > Reading > Posts page.
Then in home.php, you can add whatever you like, ie -
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name'
);
wp_list_categories( $args );
?>
To clarify, if home.php doesn't exist in your theme, WP will then look for index.php.
There are same query post for page.php,single.php because it will just fetch content. you can differentiate by as follows.
Home Page display add same code (query post) in fallowing
1) home.php
2)index.php
front-page.php – Used for both “your latest posts” or “a static page”
as set in the front page displays section of Settings → Reading.
home.php – If WordPress cannot find front-page.php and “your latest
posts” is set in the front page displays section, it will look for
home.php. Additionally, WordPress will look for this file when the
posts page is set in the front page displays section.
page.php – When “front page” is set in the front page displays
section. index.php – When “your latest posts” is set in the front
page displays section but home.php does not exist or when front page
is set but page.php does not exist.
Single Post
The single post template file is used to render a single post. WordPress uses the following path:
single-{post-type}-{slug}.php – (Since 4.4) First, WordPress looks
for a template for the specific post. For example, if post type is
product and the post slug is dmc-12, WordPress would look for
single-product-dmc-12.php.
single-{post-type}.php – If the post type is product, WordPress would
look for single-product.php.
single.php – WordPress then falls back to single.php.
singular.php – Then it falls back to singular.php.
index.php – Finally, as mentioned above, WordPress ultimately falls
back to index.php.
Single Page
The template file used to render a static page (page post-type). Note that unlike other post-types, page is special to WordPress and uses the following patch:
custom template file – The page template assigned to the page. See
get_page_templates().
page-{slug}.php – If the page slug is recent-news, WordPress will
look to use page-recent-news.php.
page-{id}.php – If the page ID is 6, WordPress will look to use
page-6.php. page.php
singular.php
index.php
Detailed Explanation: click here

WordPress Underscores Theme Site Title Not First

The most recent Underscores theme seems to post the Page Title first then the Site Title afterwards. How can I change this to have the Site Title first?
Open your header.php theme file
Add:
<title><?php bloginfo('name'); ?> <?php if ( is_single() ) { ?> » Blog Archive <?php } ?> <?php wp_title(); ?></title>
You can remove » which looks like: » ( It's upto you )
Underscores theme is using add_theme_support() in order to support title tag as introduced in the wordpress version 4.1 and as they also mentioned in the functions.php
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( 'title-tag' );

Wordpress the_content(); showing content and attachements

I am using wordpress 3.6 and advanced custom field plugin 4.2.1.
advanced custom field settings - https://dl.dropboxusercontent.com/u/15109395/q/acf-media.png
my post - https://dl.dropboxusercontent.com/u/15109395/q/page-content.png
my single page code
<section id="contentLeft" class="single">
<?php if ( have_posts() ) { the_post(); the_content(); }?>
</section>
Result - https://dl.dropboxusercontent.com/u/15109395/q/page-front.png
but , i don't want that attachment image on content area. my local installation have no problem. not showing attachments. but my live website showing attachment uploaded to that post.
how can i solve this issue? please help me. Thanks.
My problem solved with this solution. Thanks.
http://wordpress.org/support/topic/thumbnails-magically-appearing-on-all-pages
if you want remove images from all posts and pages, simple copy the following code into your theme functions.php
<?php
function remove_images( $content ) {
$postOutput = preg_replace('/<img[^>]+./','', $content);
return $postOutput;
}
add_filter( 'the_content', 'remove_images', 100 );
?>
it will remove the images wherever the_content() have images
else you need to include this function in specific page , put the following code
<?php remove_filter( 'the_content', 'remove_images' ); ?>

Resources